home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / PROLOG / HUMBOLT / HUMBOLTS / _files / _humboltsr / PROLOG._c < prev    next >
Text File  |  1990-12-08  |  11KB  |  407 lines

  1. /***************************************************
  2. ****************************************************
  3. **                                                **
  4. **  HU-Prolog     Portable Interpreter System     **
  5. **                                                **
  6. **  Release 1.62   January  1990                  **
  7. **                                                **
  8. **  Authors:      C.Horn, M.Dziadzka, M.Horn      **
  9. **                                                **
  10. **  (C) 1989      Humboldt-University             **
  11. **                Department of Mathematics       **
  12. **                GDR 1086 Berlin, P.O.Box 1297   **
  13. **                                                **
  14. ****************************************************
  15. ***************************************************/
  16.  
  17. #include <stdlib.h>
  18. #include <setjmp.h>
  19.  
  20. #include "systems.h"
  21. #include "types.h"
  22. #include "errors.h"
  23. #include "atoms.h"
  24. #include "files.h"
  25. #include "manager.h"
  26.  
  27. IMPORT void InitIO();                   /* from basicio.c */
  28. IMPORT void InitAtoms();                /* from atomtable.c */
  29. IMPORT void InitMemory();               /* from memory.c */
  30. IMPORT void InitDatabase();             /* from database.c */
  31. IMPORT void InitdynMem();                  /* from memory.c */
  32. IMPORT void InitAll();
  33. #if WINDOWS
  34. IMPORT void w_init();
  35. #endif
  36. #if USER
  37. IMPORT void InitUser();                  /* from usereval.c   */
  38. #endif
  39.  
  40. IMPORT void retractclauses();            /* from retract.c */
  41. IMPORT TERM READIN();                    /* from readin.c */
  42. IMPORT int VARCOUNT;                     /* from readin.c */
  43. IMPORT int IOERRORFLAG;
  44. IMPORT int ERRORFLAG;
  45. IMPORT ENV NEWENV();                    /* from unify.c */
  46. IMPORT void KILLSTACKS();               
  47. IMPORT void ABORT(),SYSTEMERROR();      /* from linebufffer.c */
  48. IMPORT file OpenFile();                 /* from files.c */
  49. IMPORT boolean FileExist();
  50. IMPORT void FileError();
  51. IMPORT ENV TRACE_GOON;                   /* from writeout.c */
  52. IMPORT TERM A0,A2;                       /* from evalpreds.c     */
  53. IMPORT TERM CALLX;                       /* from eval.c */
  54. IMPORT ATOM LOOKUP();
  55. IMPORT void exit();
  56. IMPORT string s_gotoxy();
  57. IMPORT TERM mk2sons(),VARTERM(),
  58.             phy_name(), stackterms();
  59. IMPORT ATOM copyatom();
  60. IMPORT CLAUSE ADDCLAUSE();
  61. IMPORT boolean EXECUTE();
  62. #if REALARITH
  63. IMPORT boolean FpAbort;                  /* from arith.c         */
  64. #endif
  65.  
  66. /* Variable declarations */
  67.  
  68. GLOBAL PHASE  MODE;
  69. LOCAL boolean RST;
  70.  
  71. #if INITFILE
  72. GLOBAL string RESTORESTATE=RESFILE;
  73. #endif
  74. LOCAL string PROLIB= (string)0;
  75.  
  76. ENV  E,CHOICEPOINT;
  77. TERM BE;
  78. GLOBAL int ARGC;
  79. GLOBAL char **ARGV;
  80. int SPYING=0;            /* count spypoints */
  81. boolean  HALTFLAG=false, 
  82.          TRACING=false, 
  83.          SPYTRACE=false,
  84.          ECHOFLAG=false,
  85.          DEBUGFLAG=false,
  86.          OCHECK  =false,  
  87.          WARNFLAG=true,
  88.          ENAB_INTR=true,
  89.          EVENT=false,
  90.          xWINDOW_ON=false,
  91.          UserAbort=false,
  92.          aSYSMODE=false,
  93.          REDUCEFLAG=true,
  94.          VERBOSE=true,
  95.          BOOTING=false,
  96.          In_Toplevel_Read=false;
  97.  
  98.  
  99.  
  100.  
  101.  
  102. /* encapsulation of jump to global labels 100,101,999 */
  103.  
  104. jmp_buf error_label;  /* used also in evalpreds.c */ 
  105. jmp_buf abort_label;
  106. LOCAL globaladdr=0;
  107.  
  108. GLOBAL int RETURN_CODE=0;
  109.  
  110. GLOBAL void ERRORJMP (void)
  111. {
  112.   longjmp(error_label,1); 
  113. }
  114.  
  115. GLOBAL void LONG_JMP(int I)
  116. {
  117.   longjmp(abort_label,I);
  118. }
  119.  
  120. #if !CPM
  121. #include <signal.h>
  122. LOCAL void sig_handler(int s)
  123. {
  124.   switch(s) {
  125. #if REALARITH && VMS
  126.   case SIGFPE:
  127.          signal(FPEXCEPTE,sig_handler);
  128.          FpAbort=true;break;
  129. #endif
  130.   case SIGINT:
  131.          signal(SIGINT,sig_handler);
  132.          EVENT=true; UserAbort=true; 
  133.   if(MODE!=USERM) LONG_JMP(999);  
  134.   break;
  135. #if UNIX || VMS
  136.   case SIGQUIT: LONG_JMP(999);
  137. #endif
  138.   }
  139. }
  140. #endif
  141.  
  142. /*
  143.    Read and execute clauses from the current file. 
  144. */
  145.  
  146. LOCAL TERM MAKETOPLEVEL(TERM X)
  147. { TERM Y;
  148.     if(name(X)==COMMA_2)
  149.     { 
  150.         X=son(X); 
  151.         return mk2sons(name(X),son(X),GOTO_1, MAKETOPLEVEL(br(X)));
  152.     }
  153.     else if(non_nil_term(Y=VARTERM())) 
  154.             return mk2sons(name(X),son(X),GOTO_1,Y);
  155.     else return mk2sons(name(X),son(X),nil_atom,nil_term);
  156.  
  157. ENV TOPENV;  
  158.  
  159. LOCAL void TOPLEVEL (PHASE MO, boolean init)
  160. { TERM  X;
  161.   CLAUSE MCL;
  162.   ATOM filename;
  163.   ATOM LAST_ASS_ATOM=nil_atom;
  164.   MODE=MO;
  165.   HALTFLAG=false;
  166.   TRACE_GOON=0;
  167.   switch (MO)
  168.   { case SYSM:  filename=LOOKUP(PROLIB,0,true); break;
  169.     case USERM: filename=STDIN_0; break;
  170.   }
  171.   if((inputfile=OpenFile(phy_name(filename),read_mode)) < 0)
  172.   {
  173.         FLOGNAME(inputfile)=STDIN_0;
  174.         if(MODE==USERM) inputfile=STDIN;
  175.         else {CALLX=phy_name(filename);FileError(CANTOP);ABORT(0);}
  176.   }
  177.   else 
  178.       FLOGNAME(inputfile)=copyatom(filename);
  179.   if((outputfile=OpenFile(phy_name(STDOUT_0),write_mode)) < 0)
  180.         if(MODE==USERM) outputfile=STDOUT;
  181.         else {CALLX=phy_name(STDOUT_0);FileError(CANTOP);ABORT(0);}
  182.   FLOGNAME(outputfile)=STDOUT_0;
  183.   do
  184.   {
  185.     retractclauses();
  186.     TOPENV=CHOICEPOINT=E=NEWENV(0); BE=base(E);
  187.     
  188.     if (init && non_nil_clause(clause(INIT_0))) 
  189.       { EXECUTE(mkfunc(CALL_1,mkatom(INIT_0)),E); init=0; }
  190.     else
  191.     if(MODE==USERM)
  192.       if(non_nil_clause(clause(TOP_0))) 
  193.         EXECUTE(mkfunc(CALL_1,mkatom(TOP_0)),E);
  194.       else 
  195.         { if(VERBOSE)
  196.             if (non_nil_clause(clause(PROMPT_0)))
  197.              { EXECUTE(mkfunc(CALL_1,mkatom(PROMPT_0)),E);
  198.                KILLSTACKS(TOPENV);
  199.                TOPENV=CHOICEPOINT=E=NEWENV(0); BE=base(E);
  200.              }
  201.             else ws("?-");
  202.           In_Toplevel_Read=true;X=READIN();In_Toplevel_Read=false; 
  203.           clause(MAIN_0)=MCL=stackterms(5);
  204.           name(MCL)=CLAUSET; name(br(MCL))=INTT;
  205.           nextcl(MCL)=nil_clause; setnvars(MCL,VARCOUNT);
  206.           name(head(MCL))=MAIN_0; son(head(MCL))=nil_term;
  207.           name(body(MCL))=GOTO_1; son(body(MCL))=MAKETOPLEVEL(X);
  208.           if(EXECUTE(mkatom(MAIN_0),E)) {if(VERBOSE)ws("\nyes\n");}
  209.           else if(VERBOSE)ws("\nno\n");
  210.         }
  211.     else { 
  212.            if(!ECHOFLAG && VERBOSE && MODE!=SYSM)ws("."); 
  213.            X=READIN();
  214.            if(name(X)==END_0) HALTFLAG=true;
  215.            else if(name(X)==QUESTION_1 || name(X)==ARROW_1)
  216.            {
  217.                 LAST_ASS_ATOM=nil_atom;
  218.                 if(!EXECUTE(mkfunc(CALL_1,son(X)),E) &&
  219.                    name(X)!=ARROW_1 && WARNFLAG)
  220.                     ws("WARNING: goal failed during consult/reconsult");
  221.            }
  222.            else 
  223.            {    /* assertz(X) */ 
  224.                 register ATOM A;
  225.                 register CLAUSE CL,CX;
  226.                 
  227.                 if((A=name(X))==ARROW_2) A=name(arg1(X));
  228.                 if((system(A) && !aSYSMODE) || class(A)!=NORMP)
  229.                     ABORT(SYSPROCE);
  230.                 A=copyatom(A);
  231.                 if(non_nil_clause(CL=clause(A)))
  232.                 {   while(non_nil_clause(CX=nextcl(CL))) CL=CX;
  233.                     nextcl(CL)=CX=ADDCLAUSE(X);
  234.                     if(WARNFLAG && LAST_ASS_ATOM !=A)
  235.                     {
  236.                         ws("WARNING: new clauses for ");
  237.                         wq(A);ws("/");wi(arity(A));
  238.                         ws("\n");
  239.                     }
  240.  
  241.                 }
  242.                 else clause(A)=CX=ADDCLAUSE(X);
  243.                 nextcl(CX)=nil_clause;
  244.  
  245.                 LAST_ASS_ATOM=A;
  246.            }
  247.          }
  248.     KILLSTACKS(TOPENV);
  249.     if(MODE==USERM) IOERRORFLAG=0; 
  250.     if(UserAbort && ENAB_INTR) ABORT(ABORTE);
  251.   }
  252.   while(!HALTFLAG);
  253. }
  254.  
  255. LOCAL void InitArg(int argc, char *argv[])
  256. {int i;
  257.  for(i=1;i<argc;i++)
  258.    if (argv[i][0]=='-') {
  259.      if( i < argc-1 )
  260.         switch(argv[i][1]) {
  261.           case 'l' : case 'L' : PROLIB=argv[++i];break;
  262. #if INITFILE
  263.           case 'r' : case 'R' : RESTORESTATE=argv[++i];break;
  264. #endif
  265. #ifdef DYNMEM
  266.           case 'a' : case 'A' : MAX_ATOMS=atoi(argv[++i]);break;
  267.           case 't' : case 'T' : MAX_TERMS=atoi(argv[++i]);break;
  268.           case 'c' : case 'C' : MAX_TRAILER=atoi(argv[++i]);break;
  269.           case 'g' : case 'G' : MAX_ENVS=atoi(argv[++i]);break;
  270.           case 's' : case 'S' : MAX_STRINGS=atoi(argv[++i]);break;
  271. #endif
  272.        }
  273.  
  274.      switch(argv[i][1])
  275.        { case 'B' : if (argv[i][2]=='O' && argv[i][3]=='O' && 
  276.                       argv[i][4]=='T' && argv[i][5]==0) 
  277.                   BOOTING=true; break;
  278.          case 'v' : case 'V' : VERBOSE=false;break;
  279.        }
  280.    }
  281.      
  282. }
  283.  
  284. #ifdef OYSTER
  285. LOCAL string RCNAME=OYSTERRC;
  286. #else
  287. LOCAL string RCNAME=PROLOGRC;
  288. #endif
  289.  
  290. LOCAL void rcfile(void)
  291. {
  292.     string S,A,I;
  293.     extern char *getenv();                       /* from clib */
  294.     static char AA[100];
  295.     A="";
  296. #if RISCOS
  297.     S=getenv( "HUPro$RC" );
  298.     if( S == 0 )
  299.        S = RCNAME;
  300.     if(FileExist(S))
  301.       A = S;
  302. #else
  303.     if(FileExist(RCNAME)) A=RCNAME; 
  304. #if UNIX 
  305.     else
  306.       { A=AA;
  307.         S=getenv("HOME");I=A; while(*S) *I++= *S++;
  308.         if(*(--S) != '/') *I++= '/';
  309.         S=RCNAME; while(*S) *I++= *S++;
  310.         *I= '\0';
  311.         if(!FileExist(A)) return ;
  312.       }
  313. #endif
  314. #endif
  315.   if(!PROLIB && *A)PROLIB=A;
  316.   return ; 
  317. }
  318.  
  319. #ifdef OYSTER
  320. LOCAL string Copyright="\nOyster-2 Programming System 0.7 24/04/90\n\n";
  321. #else
  322. LOCAL string Copyright="\nHU-Prolog Copright (c) 1990 C.Horn, M.Dziadzka, M.Horn\nRISC-OS VERSION 1.1\n\n";
  323. #endif
  324.  
  325. GLOBAL void DOVERSION(void)
  326. { ws(Copyright); 
  327. }
  328.  
  329. int main(int argc, char *argv[])
  330.   int first_time=0;
  331.   ARGC=argc;
  332.   ARGV=argv;
  333.   InitArg(argc,argv);
  334. #ifdef DYNMEM
  335.   InitdynMem();
  336. #endif
  337.   if (!BOOTING && first_time==0) InitAll();
  338.   if (BOOTING) InitMemory(); 
  339. #if  BIC
  340.   Init_Irm();
  341. #endif
  342. #if WINDOWS
  343.   if(xWINDOW_ON)w_init();
  344. #endif
  345. #if !CPM
  346.   if (!BOOTING) rcfile();
  347. #endif
  348.   if( !PROLIB) PROLIB= "\0";
  349.  
  350.   /* Initialising Interrupt Handling */
  351. #if !CPM
  352.   signal(SIGINT,sig_handler);     /* user interrupt */
  353. #if UNIX || VMS
  354.   signal(SIGQUIT,sig_handler);    /* kill process */
  355. #endif
  356. #if VMS && REALARITH
  357.   signal(SIGFPE,sig_handler);     /* floating point exception */
  358. #endif
  359. #endif
  360.  
  361.   globaladdr=setjmp(abort_label);
  362.   InitIO();
  363.   ERRORFLAG=0;
  364.   (void)setjmp(error_label);
  365.   if(ERRORFLAG) ABORT(ERRORFLAG);
  366.   UserAbort=false;
  367.  
  368.   if( VERBOSE ) ws(Copyright); 
  369.  
  370.   if(RST)goto l101;
  371.   EVENT=SPYTRACE;
  372.   if(globaladdr==100) goto l100;
  373.   if(globaladdr==101) goto l101;
  374.   if(globaladdr==999) goto l999;
  375.   if (BOOTING)
  376.   { 
  377. #if USER
  378.     InitUser(0);
  379. #endif
  380.     InitAtoms(); 
  381.     InitDatabase(); 
  382. #if USER
  383.     InitUser(1);
  384. #endif
  385.   }
  386.  
  387.      aSYSMODE=true;
  388.      if(*PROLIB) TOPLEVEL(SYSM,0);
  389.   l100:
  390.      aSYSMODE=false;
  391.   l101:
  392.      globaladdr=101; 
  393.      if(first_time++==0) TOPLEVEL(USERM,1); 
  394.      else TOPLEVEL(USERM,0);
  395.   l999:
  396.      outputfile=STDOUT;
  397. #if WINDOWS
  398.      if(xWINDOW_ON)w_exit();
  399. #endif
  400.   exit(RETURN_CODE);
  401.   /*NOTREACHED*/
  402. }
  403.  
  404.  
  405.